package mytest;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedOutputStream;
import java.util.ArrayList;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

public class ObjectParser implements Runnable {
	private InputStream inputStream = null;
	PipedOutputStream outputStream = null;
	private ArrayList persons = null;
	private XMLStreamWriter writer = null;

	public ObjectParser(InputStream objReceive, PipedOutputStream outputStream) {
		this.inputStream = objReceive;
		this.outputStream = outputStream;
	}

	@Override
	public void run() {
		persons = receiveObjects(inputStream);
	}

	public void modify(ArrayList persons) {
		ArrayList newArrPersons = new ArrayList<>();
		System.out.println("old parameters");
		for (int i = 0, lenI = persons.size(); i < lenI; i++) {
			Person person = (Person) persons.get(i);
			
			System.out.println(person.getNume());
			System.out.println(person.getPrenume());
			System.out.println(person.getAge());
			
			person.setAge(111);
			newArrPersons.add(person);
		}
		
		ObjectOutputStream oo;
		try {
			oo = new ObjectOutputStream(this.outputStream);
			oo.writeObject(newArrPersons);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

	public ArrayList receiveObjects(InputStream in) {

		try {
			ObjectInputStream oi = new ObjectInputStream(in);
			ArrayList<Person> readob = (ArrayList) oi.readObject();
			modify(readob);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
